from OWRpy import *
import redRi18n
_ = redRi18n.get_(package = 'base')

import signals 
import redRGUI 

class cenTransform(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        
        # Unique R variable
        self.setRvariableNames(["centered"])
        
        # Python variables
        self.data = {}
        self.RFunctionParam_x = ''
        
        # Input
        self.inputs.addInput("x", "x", signals.base.RMatrix, self.processx)
        
        # Output
        self.outputs.addOutput("cenTransform Output","cenTransform Output", signals.base.RMatrix)
        
        # GUI
        self.dataClass = redRGUI.base.lineEdit(self.controlArea, label = "Data class:", text = '')
        self.dataClass.setDisabled(True)
        self.centOpLabel = redRGUI.base.lineEdit(self.controlArea, label = "Centered operation:", text = '')
        self.centOpLabel.setDisabled(True)
        #self.centOpCommand = 'clr' # don't need to specify command here. Command "cdt" is general and look for the class to redirect to adequate command
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignRight, 
        callback = self.commitFunction, processOnInput=True)


        
    def processx(self, data):
        if not self.require_librarys(["compositions"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_x=data.getData()
            #self.data = data
            self.dataClass.setText(self.R('class('+self.RFunctionParam_x+')'))
            
            if self.dataClass.text() =='rplus' :
                self.centOpLabel.setText('Isometric identity transform')
                #self.centOpCommand = 'iit'
            elif self.dataClass.text() =='aplus' :
                self.centOpLabel.setText('Isometric log transform')
                #self.centOpCommand = 'ilt'
            elif self.dataClass.text() =='rcomp' :
                self.centOpLabel.setText('Centered planar transform')
                #self.centOpCommand = 'cpt'
            elif self.dataClass.text() =='acomp' :
                self.centOpLabel.setText('Centered log-ratio transform')
                #self.centOpCommand = 'clr'
            else :
                self.status.setText('The class of the input is not compositional. Please parse data though widget "Close the simplex".')
                return
            self.commitFunction()
        else:
            self.RFunctionParam_x=''

    def commitFunction(self):
        if str(self.RFunctionParam_x) == '':
            self.status.setText('Data is missing.')
            return
        
        self.R(self.Rvariables['centered']+'<-cdt(x='+str(self.RFunctionParam_x)+')', processingNotice=True)
        newData = signals.base.RMatrix(self, data = self.Rvariables["centered"], checkVal = False)
        self.rSend("cenTransform Output", newData)